#!/bin/bash
#!/usr/bin/env bash
usage() {
    pname=$(basename $0)
    echo "$pname --was_home <was install path> --ims_home <ims install path> --was_filepath <was filepath> --run_user <run user> --admin_userName <admin userName> --admin_password <admin password>"
    exit -1
}

parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            was_home)
                was_home="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            ims_home)
                ims_home="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            was_filepath)
                was_filepath="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            run_user)
                run_user="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            admin_userName)
                admin_userName="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            admin_password)
                admin_password="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"


if [[  ! -n "$was_home" ]] ; then
    echo "ERROR:: Must defined was_home."
    usage
fi

if [[  ! -n "$ims_home" ]] ; then
    echo "ERROR:: Must defined ims_home."
    usage
fi

if [[ ! -d "$was_home" ]]; then 
    echo "ERROR:: $was_home directory not found."
    exit 1
fi 

if [[ ! -d "$ims_home" ]]; then 
    echo "ERROR:: $ims_home directory not found."
    exit 1
fi 

if [[  ! -n "$was_filepath" ]] ; then
    echo "ERROR:: Must defined was_filepath."
    usage
fi 

if [[  ! -n "$run_user" ]] ; then
    echo "ERROR:: Must defined run_user."
    usage
fi 


#判断是否存在websphere安装目录
if [[  ! -d "$was_home/lib"  || ! -d "$was_home/bin" ]] ; then
    echo "ERROR:: $was_home not websphere install path."
    exit 1
fi

timeout=120

#检查应用概要
for dir in `ls $was_home/profiles/`
do
    #echo $dir
    if [[ "$dir" =~ "Dmgr" ||  "$dir" =~ "dmgr" ]]; then 
        dmgr_home=$was_home/profiles/$dir/
    else
        for server in `ls $was_home/profiles/$dir/servers/` 
        do 
            is_server=`ps -ef | grep websphere |grep $dir | grep $server |awk '{print $2}'`
            if [[ -n "$is_server" ]] ; then 
                echo "INFO:: Stop $dir Profile Server $server start."

                if [[ -n "$admin_userName" && -n "$admin_password" ]] ; then 
                    sh $was_home/profiles/$dir/bin/stopServer.sh $server -username $admin_userName -password $admin_password
                else 
                    sh $was_home/profiles/$dir/bin/stopServer.sh $server
                fi 
                echo "FINE:: Stop $dir Profile Server $server end."
            fi
        done
    fi 
done

#检查DMGR进程
is_admin=`ps -ef | grep websphere |grep Dmgr |awk '{print $2}'`
if [[ -n "$is_admin" ]] ; then 
    echo "INFO:: Stop Dmgr Profile start."
    cmd=""
    if [[ -n "$admin_userName" && -n "$admin_password" ]] ; then 
        sh $dmgr_home/bin/stopManager.sh  -trace  -nowait -username $admin_userName -password $admin_password
    else 
        sh $dmgr_home/bin/stopManager.sh  -trace  -nowait
    fi 
    sh $cmd
    echo "FINE:: Stop Dmgr Profile end."
fi 

sleep 20 

#检查所有的进程是否已停完成，没停完成强制kill
for pid in `ps -ef | grep websphere | grep -E 'server|Dmgr|AppSvr|nodeagent' | grep -v grep | awk '{print $2}'`
do
    echo "INFO:: kill -9 $pid"
    kill -9 $pid
done

was_ver=`su - "$run_user"  -c "$ims_home/tools/imcl listAvailablePackages -repositories $file_path -silent"`
if [ $? != 0 ]
then
    echo "ERROR:: Get webSphere detail version failed."
    exit 1
fi

if [[ -n "$was_ver" ]] ; then 
    echo "INFO:: Get webSphere detail version : $was_ver"
else 
    echo "ERROR:: Get webSphere detail version empty."
    exit 1
fi

if [[ -d "$was_home/uninstall" ]]; then 
    echo "INFO:: Websphere uninstall start."
    sh $ims_home/tools/imcl uninstall $was_ver -installationDirectory $was_home -showProgress -silent
    echo "FINE:: Websphere uninstall end."
fi 

#清理home用户环境变量
cd /home/$run_user/
if [ $? == 0 ]
then
    rm -rf /home/$run_user/etc
    rm -rf /home/$run_user/var
    rm -rf /home/$run_user/.ibm
    rm -rf /home/$run_user/.java
    rm -rf /home/$run_user/.local
    rm -rf /home/$run_user/.eclipse
    rm -rf /home/$run_user/.config
fi

#清理安装目录
echo "INFO:: Websphere clean install directory start."
rm -rf $was_home
rm -rf $(dirname "$was_home")/IMShared
rm -rf $(dirname "$ims_home")/license
rm -rf $(dirname "$ims_home")/properties
rm -rf $ims_home
echo "FINE:: Websphere clean install directory end."

exit 0
